package cn.sirun.com.fca.car.status.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import cn.sirun.com.fca.car.status.R;
import cn.sirun.com.fca.car.status.listener.MyItemLongClickListener;
import cn.sirun.com.fca.car.status.model.CarStatusData;

import static com.tencent.cloud.iov.util.ResourcesUtils.getResources;

/**
 * Descrip:
 * Author: Administrator
 * Time: 2018/11/27
 * Version:
 */

public class CarStatusAdapter extends RecyclerView.Adapter<CarStatusAdapter.ViewHolder> {

    private List<CarStatusData> carStatusData;
    private LayoutInflater inflater;
    private Context mContext;
    private MyItemLongClickListener listener;
    public void setListener(MyItemLongClickListener listener) {
        this.listener = listener;
    }
    public CarStatusAdapter(List<CarStatusData> carStatusData, Context context) {
        this.carStatusData = carStatusData;
        this.mContext = context;
        this.inflater = LayoutInflater.from(context);
    }
    public List<CarStatusData> getCarStatusData(){
        return carStatusData;
    }
    public void setCarStatusData(List<CarStatusData> carStatusData) {
        this.carStatusData = carStatusData;
    }
    @Override
    public CarStatusAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.car_status_data_items,null,false);
        return new ViewHolder(view);
    }
    @Override
    public void onBindViewHolder(CarStatusAdapter.ViewHolder holder, final int position) {
        CarStatusData csd = carStatusData.get(position);
        holder.car_status_name.setText(csd.getStatusName());
        if(csd.getStatusValue().equals("异常")){
            holder.car_status_value.setTextColor(getResources().getColor(R.color.red_33));
            holder.car_status_value.setText(csd.getStatusValue());
        }else {
            holder.car_status_value.setText(csd.getStatusValue());
        }

        if (listener != null) {
            holder.rootView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    listener.onItemLongClick(v, position);
                }
            });
        }
    }
    @Override
    public int getItemCount() {
        return carStatusData == null ? 0 : carStatusData.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder {
        public View rootView;
        public TextView car_status_name;//数据参数
        public TextView car_status_value;//参数值

        public ViewHolder(View rootView) {
            super(rootView);
            this.rootView = rootView;
            this.car_status_name = (TextView) rootView.findViewById(R.id
                    .tv_car_status_name);
            this.car_status_value = (TextView) rootView.findViewById(R.id
                    .tv_car_status_data);
        }

    }

}
